package com.xhtranslate.common.filter;

import com.alibaba.fastjson.JSONObject;
import com.xhtranslate.common.config.TranslateConfig;
import com.xhtranslate.common.util.JudgeCreditLimitUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author liuxuhuang
 * @version 1.0
 * @package com.xhtranslate.common.aspect
 * @date 2022/11/20 22:40:01
 * @description 文本翻译判断是否超额
 */
@Slf4j
@Component
public class TextTranslateFilter implements Filter {
    @Autowired
    private JudgeCreditLimitUtil judgeCreditLimitUtil;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //HttpServletRequest request = (HttpServletRequest) servletRequest;
        //String uri = request.getRequestURI();
        //JSONObject object = getParamer(request);
        //log.error("object==" + object);
        //String context = object.getString("context");
        //String sufferUrl = uri.substring(0, uri.lastIndexOf("/"));
        //if (ifBeyondCredit(sufferUrl, context.length()) == false) {
        //    servletRequest.getRequestDispatcher("/overCreditReturn").forward(servletRequest, servletResponse);
        //}
        filterChain.doFilter(servletRequest, servletResponse);
    }


    /**
     * 是否超出额度 ,true未超出,false超出
     *
     * @param sufferUrl
     * @return boolean
     */
    public boolean ifBeyondCredit(String sufferUrl, int length) {
        boolean flag = false;
        switch (sufferUrl) {
            case "/baiduTranslate":
                flag = judgeCreditLimitUtil.getBeyondTextCredit(TranslateConfig.BaiduTextTranslateRedisName, TranslateConfig.BaiduTextTranslateRedisCreditLimit, length);
                break;
            case "/aliTranslate":
                flag = judgeCreditLimitUtil.getBeyondTextCredit(TranslateConfig.AliTextTranslateRedisName, TranslateConfig.AliTextTranslateRedisCreditLimit, length);
                break;
            case "/youdaoTranslate":
                flag = judgeCreditLimitUtil.getBeyondTextCredit(TranslateConfig.YoudaoTextTranslateRedisName, TranslateConfig.YoudaoTextTranslateRedisCreditLimit, length);
                break;
            default:
                flag = false;
                break;
        }
        return flag;
    }

    /**
     * 获取表单参数
     *
     * @param request
     * @return
     */
    public JSONObject getParamer(HttpServletRequest request) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        return JSONObject.parseObject(sb.toString());
    }
}
